{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 异常\n",
    "- 广义上分为错误和异常\n",
    "- 错误指的是可以认为避免\n",
    "- 异常是指在语法逻辑正确的前提下，出现的问题\n",
    "- 在python里，异常是一个类，可以处理和使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 异常处理\n",
    "- 不能保证程序永远正确运行\n",
    "- 但是，必须保证程序在最坏的情况下得到的问题被妥善处理\n",
    "- python中异常处理模块全部语法：\n",
    "    \n",
    "    try:\n",
    "        尝试实现某个操作\n",
    "        如果没出现异常，任务就可以完成\n",
    "        如果出现异常，将异常从当前代码块扔出去尝试解决异常\n",
    "    except 异常类型1:\n",
    "        解决方案1:用于尝试在此处处理异常解决问题\n",
    "    except 异常类型2:\n",
    "        解决方案2:用于尝试在此处处理异常解决问题\n",
    "        \n",
    "     except （异常类型1，异常类型2）:\n",
    "        解决方案:针对多个异常使用相同的处理方式\n",
    "        \n",
    "     except：\n",
    "         解决方案：所有异常的解决方案\n",
    "     else：\n",
    "         如果没有出现任何异常，将会执行此处代码\n",
    "     finally：\n",
    "         管你有没有异常都要执行的代码\n",
    "- 除except（最少一个）以外，else和finally可选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "plz input a number0\n",
      "输错了\n",
      "division by zero\n"
     ]
    }
   ],
   "source": [
    "# 简单异常案例\n",
    "try:\n",
    "    num = int(input('plz input a number'))\n",
    "    rst = 100/num\n",
    "    print('结果是：{0}'.format(rst))\n",
    "except ZeroDivisionError as e:\n",
    "    print('输错了')\n",
    "    print(e)\n",
    "    exit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 用户手动引发异常\n",
    "- 当某些情况，用户希望自己引发一个异常的时候，可以使用\n",
    "- raise关键字来引发异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i love fei\n",
      "3.141592\n",
      "ValueError\n",
      "我肯定被执行\n"
     ]
    }
   ],
   "source": [
    "# raise案例\n",
    "try:\n",
    "    print('i love fei')\n",
    "    print(3.141592)\n",
    "    # 手动引发一个异常\n",
    "    # 语法：raise ErrorClassName\n",
    "    raise ValueError\n",
    "    print('没完')\n",
    "except NameError as e:\n",
    "    print('NameError')\n",
    "except ValueError as e:\n",
    "    print('ValueError')\n",
    "except Exception as e:\n",
    "    print('有异常')\n",
    "finally:\n",
    "    print('我肯定被执行')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i love fei\n",
      "3.141592\n",
      "ChengError\n",
      "我肯定被执行\n"
     ]
    }
   ],
   "source": [
    "# raise案例-2\n",
    "# 自己定义异常\n",
    "# 需要注意：自定义异常必须是系统异常的子类\n",
    "class ChengError(ValueError):\n",
    "    pass\n",
    "try:\n",
    "    print('i love fei')\n",
    "    print(3.141592)\n",
    "    # 手动引发一个异常\n",
    "    # 语法：raise ErrorClassName\n",
    "    raise ChengError\n",
    "    print('没完')\n",
    "except NameError as e:\n",
    "    print('NameError')\n",
    "except ChengError as e:\n",
    "    print('ChengError')\n",
    "except ValueError as e:\n",
    "    print('ValueError')\n",
    "except Exception as e:\n",
    "    print('有异常')\n",
    "finally:\n",
    "    print('我肯定被执行')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
